package com.dtf.admin.organization.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dtf.admin.organization.form.SystemOrganizationForm;
import com.dtf.admin.organization.service.SystemOrganizationService;
import com.dtf.admin.organization.vo.SystemOrganization;
import com.dtf.core.common.config.BSysConstants;
import com.dtf.core.common.utils.Const;

/**
 * @Project: base
 * @Title: SystemOrganizationController.java
 * @Package com.sys.web.sysorganization.controller
 * @Description: TODO
 * @author Administrator
 * @date 2015年6月28日 下午12:59:16
 * @Copyright 2015
 * @version V1.0
 */
@Controller
@RequestMapping("/admin/systemorganization")
public class SystemOrganizationController {
	@Resource
	private SystemOrganizationService systemOrganizationService;
	
	@ResponseBody
	@RequestMapping("addOrganization")
	public Map addOrganization(HttpServletRequest request,
			HttpServletResponse response,
			RedirectAttributes redirectAttributes, SystemOrganizationForm form) {
		SystemOrganization systemOrganization = form.getSystemOrganization();
		
//		String id = (String) systemOrganizationService.add(systemOrganization);
//		
//		String path = form.getParentPath();
//		path = StringUtils.isEmpty(path) ? id : (path + "." +id);
//		systemOrganization.setPath(path);
//		systemOrganizationService.update(systemOrganization);
//		
		Map obj = new HashMap();
		obj.put("msg", "新增成功");
		obj.put("obj", systemOrganization);
		return obj;
		
	}
	
	/**
	 * @Title: save
	 * @Description: 保存机构信息
	 * @time:2015年6月28日下午1:02:20
	 * @param request
	 * @param response
	 * @param form
	 * @param redirectAttributes
	 * @return void 返回类型
	 * @throws
	 */
	@ResponseBody
	@RequestMapping("save")
	public Map save(HttpServletRequest request, HttpServletResponse response,
			SystemOrganizationForm form, RedirectAttributes redirectAttributes) {
		Map<String, Object> respJsonMap = new HashMap<String, Object>();
		try {
//			SystemOrganization systemOrganization = form
//					.getSystemOrganization();
//			systemOrganization.setStatus(1);
//			SystemOrganization parentSystemOrganization = systemOrganizationService
//					.get(systemOrganization.getParentId());
//			int level = parentSystemOrganization.getLevel();
//			level += 1;
//			systemOrganization.setLevel(level);
//			systemOrganizationService.add(systemOrganization);
			respJsonMap.put("msg", BSysConstants.OP_SAVE_SUC);
			respJsonMap.put("status", "1");
		} catch (Exception e) {
			respJsonMap.put("msg", BSysConstants.OP_SAVE_FAIL);
			respJsonMap.put("status", "2");
		} finally {
			return respJsonMap;
		}
	}

	/**
	 * @Title: update
	 * @Description: 修改
	 * @time:2015年6月28日下午1:19:08
	 * @param request
	 * @param response
	 * @param form
	 * @param redirectAttributes
	 * @return void 返回类型
	 * @throws
	 */
	@ResponseBody
	@RequestMapping("updateOrganization")
	public Map updateOrganization(HttpServletRequest request,
			HttpServletResponse response, SystemOrganizationForm form,
			RedirectAttributes redirectAttributes) {
		SystemOrganization systemOrganization = form.getSystemOrganization();
		systemOrganizationService.edit(systemOrganization);
		Map obj = new HashMap();
		obj.put("msg", "更新成功");
		obj.put("obj", systemOrganization);
		return obj;
//		BaseForm.printJson(obj, response, request);
	}

	/**
	 * @Title: delete
	 * @Description: 删除
	 * @time:2015年6月28日下午1:20:21
	 * @param request
	 * @param response
	 * @param form
	 * @param redirectAttributes
	 * @return void 返回类型
	 * @throws
	 */
	@ResponseBody
	@RequestMapping("deleteOrganization")
	public Map deleteOrganization(HttpServletRequest request,
			HttpServletResponse response, SystemOrganizationForm form,
			RedirectAttributes redirectAttributes) {
		Map<String, Object> respJsonMap = new HashMap<String, Object>();
		try {
//			String hql = "update SystemOrganization set status=-99 where organizationId=?";
//			systemOrganizationService.update(
//					hql,
//					new ArrayList<Object>(Arrays.asList(form
//							.getSystemOrganization().getOrganizationId())));
			respJsonMap.put("msg", BSysConstants.OP_DELETE_SUC);
			respJsonMap.put("status", "1");
		} catch (Exception e) {
			respJsonMap.put("msg", BSysConstants.OP_DELETE_FAIL);
			respJsonMap.put("status", "2");
		}
		return respJsonMap;
	}

	/**
	 * @Title: getsystemorganization
	 * @Description: 获取信息
	 * @time:2015年6月28日下午1:25:02
	 * @param request
	 * @param response
	 * @param form
	 * @param redirectAttributes
	 * @return void 返回类型
	 * @throws
	 */
	@ResponseBody
	@RequestMapping("getsystemorganization")
	public SystemOrganization getsystemorganization(HttpServletRequest request,
			HttpServletResponse response, SystemOrganizationForm form,
			RedirectAttributes redirectAttributes) {
		SystemOrganization systemOrganization = null;
//		try {
//			systemOrganization = this.systemOrganizationService.get(form
//					.getSystemOrganization().getOrganizationId());
//		} catch (Exception e) {
//			Log.error("查询机构信息失败,详细:" + e.getMessage());
//		}
		
		return systemOrganization;
	}
	
	/**
	 * @Title: getsystemorganization
	 * @Description: 获取信息
	 * @time:2015年6月28日下午1:25:02
	 * @param request
	 * @param response
	 * @param form
	 * @param redirectAttributes
	 * @return void 返回类型
	 * @throws
	 */
	@ResponseBody
	@RequestMapping("loadOrganizationList")
	public List loadOrganizationList(HttpServletRequest request,
			HttpServletResponse response, SystemOrganizationForm form,
			RedirectAttributes redirectAttributes) {
		List<SystemOrganization> list = null;
//		try {
//			list = this.systemOrganizationService.getAllList(SystemOrganization.class);
//		} catch (Exception e) {
//			Log.error("查询机构信息失败,详细:" + e.getMessage());
//		}
//		if (null != list) {
//			form.writeJsonByExcludesProperties(list, response,
//					request);
//		}
		return list;
	}
	@ResponseBody
	@RequestMapping("loadOrganizationTree")
	public Map loadOrganizationTree(HttpServletRequest request,
			HttpServletResponse response, SystemOrganizationForm form) {
		
		Map req = systemOrganizationService.loadOrganizationTree(form);
		
//		obj.put("records", pagination.getTotal());
//		obj.put("total", pagination.getSumPage());
//		obj.put("page", pagination.getPage());
//		obj.put("rows", pagination.getRows());
		return req;
	}
	
	@RequestMapping("edit")
	public void edit(HttpServletRequest request,
			HttpServletResponse response, SystemOrganizationForm form) throws Exception {
		
		if (form.isAdd()) {
			this.add(form);
		}else if (form.isEdit()) {
			this.edit(form);
		}else if (form.isDelete()) {
			systemOrganizationService.delete(form);
		}
		form.wirteInfo("操作成功", response);
	}
	
	private Map add(SystemOrganizationForm form)throws Exception{
		int level = 0;
		String path = "-1";
		String parentId = "-1";
		
		if (!StringUtils.isEmpty(form.getParent_id())) {
			SystemOrganization parentOrganization = new SystemOrganization();
			parentOrganization.setOrganization_id(form.getParent_id());
			parentOrganization = (SystemOrganization) systemOrganizationService.get(parentOrganization);
			parentOrganization.setIs_leaf("false");
			systemOrganizationService.edit(parentOrganization);
			
			level = Integer.valueOf(parentOrganization.getLevel())+1;
			path = parentOrganization.getPath();
			parentId = parentOrganization.getOrganization_id();
		}
		
		SystemOrganization organization = new SystemOrganization();
		organization.setOrganization_id(Const.getUUID());
		organization.setIs_leaf("true");
		organization.setStatus(form.getStatus());
		organization.setOrganization_name(form.getOrganization_name());
		organization.setLevel(level);
		organization.setParent_id(parentId);
		path = path + "." + organization.getOrganization_id();
		organization.setPath(path);
		systemOrganizationService.add(organization);
		return null;
	}
	private Map edit(SystemOrganizationForm form) throws Exception{
		SystemOrganization organization = (SystemOrganization) systemOrganizationService.get(form.getId());
		Const.copy(form, organization);
		organization.setOrganization_name(form.getOrganization_name());
		organization.setStatus(form.getStatus());
		systemOrganizationService.edit(organization);
		return null;
	}
}
